package com.winkyle.woodenfish.controller.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.WindowManager;

import com.winkyle.woodenfish.R;
import com.winkyle.woodenfish.mvpview.AppLauncherView;
import com.winkyle.woodenfish.databinding.ActivityAppLauncherBinding;
import com.winkyle.woodenfish.factory.presenterfactory.CreatePresenter;
import com.winkyle.woodenfish.presenter.AppLauncherPresenter;
import com.winkyle.woodenfish.util.RxCountDownUtil;

import rx.Subscriber;
import rx.Subscription;

/**
 * @author winkyle
 * @date 2018/06/13
 * @description 继承自 {@link AbstractMvpActivity}的活动类AppLauncherActivity，app的指导页
 */
@CreatePresenter(value = AppLauncherPresenter.class)
public class AppLauncherActivity extends AbstractMvpActivity implements AppLauncherView{
    private String TAG = "AppLauncherActivity";

    private ActivityAppLauncherBinding activityAppLauncherBinding;

    /**
     * @description 活动创建，指定layout布局
     * @param savedInstanceState 保存的活动状态的数据
     * @return
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置窗体全屏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        activityAppLauncherBinding = DataBindingUtil.setContentView(this, R.layout.activity_app_launcher);
        initActivity();
    }

    /**
     * @description 初始化活动的一些数据和动作
     * @return
     */
    public void initActivity(){
        activityAppLauncherBinding.setAppLauncherView(this);
        cuntDownFinishActivity();
    }

    private Subscription subscription;
    /**
     * @description 倒计时中显示计时过程，计时结束，结束本界面，前往登录界面
     * @return
     */
    public void cuntDownFinishActivity() {
        subscription = RxCountDownUtil.countdown(6)
                .subscribe(new Subscriber<Integer>() {
                    @Override
                    public void onCompleted() {
                        GoMain();
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(Integer integer) {
                        activityAppLauncherBinding.jump.setText("跳过  " + integer);
                    }
                });


    }

    /**
     * @description 销毁界面，释放一些资源
     * @return
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //如果计时观察者尚未结束计时，仍处于观察中，则结束计时，防止重复打开登录页
        if(!subscription.isUnsubscribed()) {
            subscription.unsubscribe();
        }
    }


    @Override
    public void GoMain(){
        startActivity(MainActivity.class);
        finish();
    }

}
